package com.parktech.sun.domain.service.news.impl;

import com.parktech.sun.domain.model.news.News;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.repository.news.NewsRepository;
import com.parktech.sun.domain.service.impl.BaseServiceImpl;
import com.parktech.sun.domain.service.news.NewsService;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * User: Think
 * Date: 11-1-12
 * Time: 下午5:08
 */
@Service("newsService")
public class NewsServiceImpl extends BaseServiceImpl<News> implements NewsService {

    private NewsRepository newsRepository;

    @Autowired
    public NewsServiceImpl(NewsRepository newsRepository) {
        super(newsRepository);
        this.newsRepository = newsRepository;
    }

    public Page<News> search(String title, Integer category, Integer publishStatus, Page page) {
        Criteria criteria = newsRepository.createCriteria();
        if (StringUtils.isNotEmpty(title)) {
            criteria.add(Property.forName("title").like(title, MatchMode.ANYWHERE));
        }
        if (category != -1) {
            criteria.add(Property.forName("category").eq(category));
        }
        if (publishStatus != -1) {
            criteria.add(Property.forName("publishStatus").eq(publishStatus));
        }

        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));

        return newsRepository.page(criteria, page);
    }

    public Page pageBy(Integer categoryId, Page page) {

        Criteria criteria = newsRepository.createCriteria();

        criteria.add(Property.forName("category").eq(categoryId));
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));

        return newsRepository.page(criteria, page);

    }

    public List<News> listTop4ImageNews() {

        Criteria criteria = newsRepository.createCriteria();
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.add(Property.forName("position").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));
        criteria.setMaxResults(4);
        return criteria.list();
    }

    public List<News> listTop7NormalNews() {

        Criteria criteria = newsRepository.createCriteria();
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.add(Property.forName("position").eq(3));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));
        criteria.setMaxResults(7);
        return criteria.list();
    }
}
